package com.supermap.dataservice.cloud.disastercloud;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年11月16 下午 5:16
 * @email 1301584033@qq.com
 */
@Mapper
public interface DisasterCloudMapper{


    /**
     * 收藏
     * */
    @Insert("insert into t_disastercollect(userId,disasterId) values(#{userId},#{disasterId})")
    int inertDiststerCollerct(@Param("userId")String userId,@Param("disasterId")String disasterId);

    /**
     * 取消收场
     * */
    @Delete("delete from t_disastercollect where userId=#{userId} and disasterId=#{disasterId}")
    int delDiststerCollerct(@Param("userId")String userId,@Param("disasterId")String disasterId);
    /**
     * 获取灾情所有的列表
     * */
    @SelectProvider(type =DisasterCloudMapperSQL.class,method = "getPageDisasterListSQL")
    List<DiststerList> getPageDisasterList(@Param("areaCode")String areaCode,@Param("userId")String userId,@Param("farmId")String farmId,@Param("pageNum")Integer pageNum,@Param("pageSize")Integer pageSise,@Param("ak")String ak,@Param("lev")Integer lev,@Param("beginTime")String beginTime,@Param("endTime")String endTime);

    /**
     * 获取灾情评论
     * */
    @Select("select t.id,t.disasterId,t.`comment`,t.userId,t.parentId,date_format(t.commentTime,'%Y-%m-%d %H:%i:%S') as commentTime from t_disastercomment t where t.disasterId=#{disasterId}  \n" +
            "order by t.commentTime desc\n" +
            "limit #{pageNum},#{pageSize}")
    List<Map<String,Object>> getPageDisasterContent(@Param("disasterId")String disasterId,@Param("pageNum")Integer pageNum,@Param("pageSize")Integer pageSise);

    @SelectProvider(type =DisasterCloudMapperSQL.class,method = "getPageDisasterListCountSQL")
    Integer  getPageDisasterListCount(@Param("areaCode")String areaCode,@Param("userId")String userId,@Param("farmId")String farmId,@Param("ak")String ak,@Param("lev")Integer lev,@Param("beginTime")String beginTime,@Param("endTime")String endTime);
    class DisasterCloudMapperSQL{
        public String getPageDisasterListSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String areaCode=(String)map.get("areaCode");
            String userId=(String)map.get("userId");
            String farmId=(String)map.get("farmId");
            int pageNum=(int)map.get("pageNum");
            int pageSize=(int)map.get("pageSize");
            Integer lev=(Integer) map.get("lev");
            String beginTime=(String)map.get("beginTime");
            String endTime=(String)map.get("endTime");
            String ak=(String)map.get("ak");
            stringBuilder.append("select t2.id as userId,\n" +
                    "t2.showName as userShowName,\n" +
                    "t2.HeadImage as userImg,\n" +
                    "t2.state as userState,\n" +
                    "t1.id as disaterId,\n" +
                    "t1.farm as disaterFarmId,\n" +
                    "t5.farmname as disaterFarmName,\n" +
                    "t1.areacode as disaAreacode,\n" +
                    "t3.areaname as disaAreaName,\n" +
                    "t1.longitude as disaLongitud,\n" +
                    "t1.latitude as disaLatitude,\n" +
                    "t1.descript,\n" +
                    "t4.code as descriptTypeCode,\n" +
                    "t4.name  as descriptTypeName,\n" +
                    "t6.puberty as pubertyName,\n" +
                    "t6.id as pubertyId,\n" +
                    "t1.`type`,\n" +
                    "t7.name as leve1Name,\n" +
                    "t1.breed,\n" +
                    "ttt1.id as levId,\n" +
                    "ttt1.name as levName,\n" +
                    "t8.name as leve2Name,\n" +
                    "date_format(t1.createtime,'%Y-%m-%d %H:%i:%S') as createtime,\n" +
                    " (select count(1) from t_disastercollect  where userId=t1.userId and disasterId=t1.id) as isDisaterCollect\n" +
                    "  ,group_concat(distinct t10.imageURLContent) as disaImgs from t_disaster t1\n" +
                    " Left join t_disaster_lev ttt1 on t1.lev=ttt1.id " );
            if(lev!=null&&lev!=0){
               stringBuilder.append("  and t1.lev="+lev+" ");
            }

            stringBuilder.append(
                    " inner join t_user t2\n" +
                    "on t1.userId=t2.id   ");
                if(areaCode!=null){
                   String areaCodes[]= areaCode.split(",");
                    stringBuilder.append(" and ( ");
                    String strs="";
                    for(String str:areaCodes) {
                        strs+="   t1.areacode like concat(" + str + ",'%') or  ";
                    }
                    stringBuilder.append(strs.substring(0,strs.length()-4));
                    stringBuilder.append("  ) ");
                }
                if(ak!=null){
                    if(userId!=null){
                        stringBuilder.append(" and t2.id="+userId+" ");
                    }
                }

                if(farmId!=null){
                    stringBuilder.append(" and t1.farm="+farmId+" ");
                }
                stringBuilder.append("and t2.del_flg=0\n" +
                        "Left join t_area t3\n" +
                        "on t1.areacode=t3.areacode\n" +
                        "Left join t_disastertype t4\n" +
                        "on t1.disastertype=t4.code\n" +
                        "Left join t_farmland t5\n" +
                        "on t1.farm =t5.id\n" +
                        "Left join  t_puberty t6\n" +
                        "on t1.growthperiod=t6.crop\n" +
                        "Left join  t_crop t7\n" +
                        "on t1.`type`=t7.id\n" +
                        "Left join  t_crop t8\n" +
                        "on t1.breed=t8.id ");
                        if(ak==null||ak.equals("1")){
                            //全查
                            stringBuilder.append(" Left");
                        }else if (ak.equals("2")){
                            //只查收藏
                            stringBuilder.append(" inner");
                        }
                        stringBuilder.append(" join (select * from t_disastercollect t9 where t9.userId="+userId+") t9\n" +
                        "on t1.id=t9.disasterId\n" +
                                " Left join t_disasterimg t10 \n" +
                                " on t1.id=t10.disasterId ");
            if(beginTime!=null&&endTime!=null){
                stringBuilder.append(" where t1.createtime  between '"+beginTime+"' and  '"+endTime+"' ");
            }
                        stringBuilder.append(" group by t1.id\n" +
                        "order by t1.createtime desc\n" +
                        "limit "+pageNum+","+pageSize+"");
                        return  stringBuilder.toString();

        }
        public String getPageDisasterListCountSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String areaCode=(String)map.get("areaCode");
            String userId=(String)map.get("userId");
            String farmId=(String)map.get("farmId");
            Integer lev=(Integer) map.get("lev");
            String beginTime=(String)map.get("beginTime");
            String endTime=(String)map.get("endTime");
            String ak=(String)map.get("ak");
            stringBuilder.append(" select count(id) from ( ");
            stringBuilder.append(" select t1.id from t_disaster t1\n" +
                    " Left join t_disaster_lev ttt1 on t1.lev=ttt1.id " );
            if(lev!=null&&lev!=0){
                stringBuilder.append("  and t1.lev="+lev+" ");
            }
            if(beginTime!=null&&endTime!=null){
                stringBuilder.append(" and t1.createtime  between '"+beginTime+"' and  '"+endTime+"' ");
            }
            stringBuilder.append(
                    " inner join t_user t2\n" +
                            "on t1.userId=t2.id   ");
            if(areaCode!=null){
                String areaCodes[]= areaCode.split(",");
                stringBuilder.append(" and ( ");
                String strs="";
                for(String str:areaCodes) {
                    strs+="   t1.areacode like concat(" + str + ",'%') or  ";
                }
                stringBuilder.append(strs.substring(0,strs.length()-4));
                stringBuilder.append("  ) ");
            }
            if(ak!=null){
                if(userId!=null){
                    stringBuilder.append(" and t2.id="+userId+" ");
                }
            }

            if(farmId!=null){
                stringBuilder.append(" and t1.farm="+farmId+" ");
            }
            stringBuilder.append("and t2.del_flg=0\n" +
                    "Left join t_area t3\n" +
                    "on t1.areacode=t3.areacode\n" +
                    "Left join t_disastertype t4\n" +
                    "on t1.disastertype=t4.code\n" +
                    "Left join t_farmland t5\n" +
                    "on t1.farm =t5.id\n" +
                    "Left join  t_puberty t6\n" +
                    "on t1.growthperiod=t6.crop\n" +
                    "Left join  t_crop t7\n" +
                    "on t1.`type`=t7.id\n" +
                    "Left join  t_crop t8\n" +
                    "on t1.breed=t8.id ");
            if(ak==null||ak.equals("1")){
                //全查
                stringBuilder.append(" Left");
            }else if (ak.equals("2")){
                //只查收藏
                stringBuilder.append(" inner");
            }
            stringBuilder.append(" join (select * from t_disastercollect t9 where t9.userId="+userId+") t9\n" +
                    "on t1.id=t9.disasterId\n" +
                    " Left join t_disasterimg t10 \n" +
                    " on t1.id=t10.disasterId " +
                    "group by t1.id\n" +
                    "order by t1.createtime desc)t1 ");
            return  stringBuilder.toString();

        }
    }


}
